package com.ourteam.system.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractServiceController;
import com.ourteam.frame.web.TreeViewNodeBean;
import com.ourteam.system.dao.ISysDomainDAO;
import com.ourteam.system.dao.ISysModuleDAO;
import com.ourteam.system.dao.SysDomainDAOQueryBean;
import com.ourteam.system.dao.SysModuleDAOQueryBean;
import com.ourteam.system.domain.ApplicationDomain;
import com.ourteam.system.domain.SystemModule;
import com.ourteam.system.domain.SystemUser;
import com.ourteam.system.service.ISysUserService;
import com.ourteam.system.service.ISystemApplicationService;

@CrossOrigin
@Controller
@RequestMapping(value="/api/SYSTEM")
public class SystemAPI extends AbstractServiceController {

	@Autowired
	private ISystemApplicationService systemApplicationService;
	
	@Autowired
	private ISysUserService sysUserService;

	@RequestMapping(value="/getSystemDomain")
	@ResponseBody
	public ApplicationDomain[] getSystemDomain(
			@RequestParam(value = "excludeIds[]", required = false) long[] excludeIds, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {

		SysDomainDAOQueryBean queryBean = new SysDomainDAOQueryBean();

		queryBean.setSortProperty(ISysDomainDAO.SortIndex);

		if (ArrayUtils.isNotEmpty(excludeIds)) {
			queryBean.createCriteria().andIdNotIn(excludeIds);
		}

		return systemApplicationService.queryApplicationDomains(queryBean);
		
	}
	
	@RequestMapping("/getSystemModuleTreeSelect")
	@ResponseBody
	public TreeViewNodeBean[] getSystemModuleTreeSelect(@RequestParam(value = "domainId") long domainId,
			@RequestParam(value = "disabledIds[]", required = false) long[] disabledIds) throws Exception {

		List<TreeViewNodeBean> treeNodeBeans = new ArrayList<TreeViewNodeBean>();

		SysModuleDAOQueryBean queryBean = new SysModuleDAOQueryBean();

		queryBean.createCriteria().andDomainIdEqualTo(domainId).andParentModuleIdEqualTo(0);

		queryBean.setSortProperty(ISysModuleDAO.SortIndex);

		SystemModule[] modules = this.systemApplicationService.querySystemModules(queryBean);

		for (int i = 0; i < modules.length; i++) {

			SystemModule module = modules[i];

			TreeViewNodeBean nodeBean = new TreeViewNodeBean();

			nodeBean.setNodeData(module);

			nodeBean.setId(String.valueOf(module.getId()));

			nodeBean.setText(module.getModuleName());

			nodeBean.setIcon(module.getModuleIcon());

			if (ArrayUtils.isNotEmpty(disabledIds)) {
				nodeBean.setDisabled(ArrayUtils.contains(disabledIds, module.getId()));
			}

			getSystemModules(domainId, module.getId(), nodeBean, disabledIds);

			treeNodeBeans.add(nodeBean);
		}

		return treeNodeBeans.toArray(new TreeViewNodeBean[treeNodeBeans.size()]);
	}

	protected void getSystemModules(long domainId, long parentModuleId, TreeViewNodeBean treeNodeBean,
			long[] disabledIds) throws Exception {

		SysModuleDAOQueryBean queryBean = new SysModuleDAOQueryBean();

		queryBean.createCriteria().andDomainIdEqualTo(domainId).andParentModuleIdEqualTo(parentModuleId);

		queryBean.setSortProperty(ISysModuleDAO.SortIndex);

		SystemModule[] systemModules = this.systemApplicationService.querySystemModules(queryBean);

		List<TreeViewNodeBean> children = new ArrayList<>();

		for (int i = 0; i < systemModules.length; i++) {

			SystemModule module = systemModules[i];

			TreeViewNodeBean nodeBean = new TreeViewNodeBean();

			nodeBean.setNodeData(module);

			nodeBean.setId(String.valueOf(module.getId()));

			nodeBean.setText(module.getModuleName());

			nodeBean.setIcon(module.getModuleIcon());

			if (ArrayUtils.isNotEmpty(disabledIds)) {
				nodeBean.setDisabled(ArrayUtils.contains(disabledIds, module.getId()));
			}

			if (treeNodeBean.isDisabled()) {
				nodeBean.setDisabled(treeNodeBean.isDisabled());
			}

			getSystemModules(domainId, module.getId(), nodeBean, disabledIds);

			children.add(nodeBean);
		}

		treeNodeBean.setNodes(children.toArray(new TreeViewNodeBean[children.size()]));

	}
	
	@RequestMapping("/getSysAccountById")
	@ResponseBody
	public SystemUser getSysAccountById(@RequestParam(value = "id", required = true) long id,
			HttpServletRequest request, Model model) throws Exception {

		return sysUserService.getSysUserById(id);

	}
	
	

}
